package com.neusoft.yuejian_01.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.yuejian_01.R;
import com.neusoft.yuejian_01.base.BaseActivity;
import com.neusoft.yuejian_01.javabean.ActivityBean;
import com.neusoft.yuejian_01.javabean.AttentionBean;
import com.neusoft.yuejian_01.javabean.IsActivityReg;
import com.neusoft.yuejian_01.javabean.IsAttentionBean;
import com.neusoft.yuejian_01.javabean.NoRegBean;
import com.neusoft.yuejian_01.listener.RetrofitListener;
import com.neusoft.yuejian_01.model.ActivityModel;
import com.neusoft.yuejian_01.model.AttentionModel;
import com.neusoft.yuejian_01.model.Const;
import com.neusoft.yuejian_01.model.ConstPhoto;

import it.sephiroth.android.library.picasso.Picasso;

public class ActivityDetailActivity extends BaseActivity implements RetrofitListener{
    private TextView activityaddressView;
    private TextView activitytimeView;
    private TextView activityphoneView;
    private TextView activitypriceView;
    private TextView activityinfoView;
    private TextView activitytitleView;
    private TextView clubnameView;
    protected Context context;
    private Button regButton;//立即报名按钮
    private ImageButton button_back,activityButton,callButton;//返回按钮、关注按钮、拨打电话按钮
    public ImageView activityImageView;
    private int a_id;
    private boolean flags;
    private boolean flagr;

    protected void initView() {
        setContentView(R.layout.activity_detail);
        activitytitleView = (TextView) findViewById(R.id.title_activity);
        activityaddressView = (TextView) findViewById(R.id.activity_address);
        activitytimeView = (TextView) findViewById(R.id.activity_time);
        activityphoneView = (TextView) findViewById(R.id.activity_phone);
        activitypriceView = (TextView) findViewById(R.id.activity_price);
        activityinfoView = (TextView) findViewById(R.id.activity_info);
        clubnameView = (TextView) findViewById(R.id.club_activity);
        activityImageView =(ImageView) findViewById(R.id.activity_image);
        callButton = (ImageButton) findViewById(R.id.call_Button);
        button_back = (ImageButton) findViewById(R.id.button_back);
        activityButton = (ImageButton) findViewById(R.id.button_attention);
        regButton=(Button) findViewById(R.id.activityreg_Button);
    }
    protected void initEvents() {
        activityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AttentionModel attentionModel = new AttentionModel();
                attentionModel.userCollectActivity(String.valueOf(getUser_id()),String.valueOf(a_id),ActivityDetailActivity.this);
            }
            });
        regButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityModel activityModel = new ActivityModel();
                activityModel.getIsNotReg(String.valueOf(getUser_id()), String.valueOf(a_id), ActivityDetailActivity.this);
            }
        });
        callButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + activityphoneView.getText().toString()));
                if (ActivityCompat.checkSelfPermission(ActivityDetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions// here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });
        button_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(ActivityDetailActivity.this,ActActivity.class);
                startActivityForResult(intent,1);
            }
        });
        }
    protected void initData() {
        a_id = getIntent().getIntExtra("a_id",-1);
        ActivityModel activityModel = new ActivityModel();
        activityModel.activityDetail(String.valueOf(a_id),this);
        AttentionModel attentionModel = new AttentionModel();
        attentionModel.isCollected(String.valueOf(getUser_id()),String.valueOf(a_id), "1",this);//flag=1。判断活动的关注状态
        ActivityModel activityModels = new ActivityModel();
        activityModels.getIsReg(String.valueOf(getUser_id()), String.valueOf(a_id), this);
    }

        @Override
    public void onSuccess(Object object,int flag) {
            switch (flag) {
                case Const.ActivityDetail:
                    //flag标识为1，获取活动详情
                    ActivityBean activityBean = (ActivityBean) object;
                    activitytitleView.setText(activityBean.getA_name());
                    activityaddressView.setText(activityBean.getA_address());
                    activitytimeView.setText(activityBean.getA_time());
                    activityphoneView.setText(activityBean.getA_phone());
                    activitypriceView.setText(String.valueOf(activityBean.getA_price()));
                    activityinfoView.setText("\u3000\u3000" + activityBean.getA_info());
                    clubnameView.setText(activityBean.getC_name());
                    final String activityPic = ConstPhoto.BASE_URL + "activitypicture/" + activityBean.getA_picture();
                    Picasso.with(context)
                            .load(activityPic)
                            .placeholder(R.drawable.pic2)
                            .into(activityImageView);
                    break;
                case Const.IsAttention:
                    //flag标识为2，判断关注状态
                    IsAttentionBean isAttentionBean = (IsAttentionBean) object;
                    if (isAttentionBean.getCollected().equals("1")) {
                        //已关注
                        flags = true;
                        activityButton.setImageResource(R.drawable.ic_attention_checked);
                    } else {
                        //未关注
                        flags = false;
                        activityButton.setImageResource(R.drawable.ic_attention);
                    }
                    break;
                case Const.IsActivityReg:
                    //flag标识为3，判断报名状态
                    IsActivityReg isActivityReg = (IsActivityReg) object;
                    if (String.valueOf(isActivityReg.getAn_paystate()).equals("1")) {
                        //去支付
                        flagr = true;
                        regButton.setText("取消报名");
                    }

                    if (String.valueOf(isActivityReg.getAn_paystate()).equals("0")) {
                        //已支付
                        regButton.setText("查看订单");
                        regButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent();
                                intent.setClass(ActivityDetailActivity.this, ActivityOrderActivity.class);
                                intent.putExtra("a_id", a_id);
                                startActivityForResult(intent, 1);
                            }
                        });
                    }
                    if (String.valueOf(isActivityReg.getAn_paystate()).equals("2")) {
                        //未报名
                        flagr = false;
                        regButton.setText("立即报名");
                        regButton.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent();
                                intent.setClass(ActivityDetailActivity.this, ActivityRegActivity.class);
                                intent.putExtra("a_id", a_id);
                                startActivityForResult(intent, 1);
                            }
                        });
                    }
                    break;
                case Const.ActivityAttention:
                    //关注活动
                    AttentionBean attentionBean = (AttentionBean) object;
                    if (attentionBean.getSuccess().equals("1")) {
                        Log.e(attentionBean.getSuccess(), "---------");
                        if (flags) {
                            flags = false;
                            activityButton.setImageResource(R.drawable.ic_attention);
                        } else {
                            flags = true;
                            activityButton.setImageResource(R.drawable.ic_attention_checked);
                        }
                        Toast.makeText(ActivityDetailActivity.this, "OK", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case Const.NoReg:
                    NoRegBean noRegBean = (NoRegBean) object;
                    if (noRegBean.getSuccess().equals("1")) {
                        Log.e(noRegBean.getSuccess(), "---------");
                        if (flagr) {
                            flagr = true;
                            regButton.setText("立即报名");
                            regButton.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent();
                                    intent.setClass(ActivityDetailActivity.this, ActivityRegActivity.class);
                                    intent.putExtra("a_id", a_id);
                                    startActivityForResult(intent, 1);
                                }
                            });
                        }
                        Toast.makeText(ActivityDetailActivity.this, "取消报名成功", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }

    @Override
    public void onFail() {
        Toast.makeText(this,"fail",Toast.LENGTH_LONG).show();

    }

    @Override
    protected void onResume() {
        super.onResume();
        initView();//初始化
        initEvents();//点击事件处理
        initData();//

    }
}
